package com.huihuiyun.huihuiyun.server.service;

import com.huihuiyun.huihuiyun.server.domain.HhyBook;
import com.huihuiyun.huihuiyun.server.repository.HhyBookRepository;
import com.mark.util.CommonUtil;
import com.mark.util.JaxrsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;

@Component
@Path("/hhyBooks")
@Slf4j
public class HhyBookService {
    @Autowired
    private HhyBookRepository hhyBookRepository;

    @Context
    private HttpServletResponse httpServletResponse;

    private static AtomicLong atomicLong = new AtomicLong(0);

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyBook post(HhyBook book) {
        log.error("----------->add book" + atomicLong.incrementAndGet());
        try {
            book.id = CommonUtil.createRandomId();
            book = hhyBookRepository.save(book);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.CREATED.getStatusCode());
            return book;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/branch")
    public void post(List<HhyBook> bookList) {
        try {
            hhyBookRepository.save(bookList);
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @DELETE
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public void delete(@PathParam("id") String id) {
        try {
            hhyBookRepository.delete(id);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
        }
    }

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public HhyBook put(HhyBook book) {
        try {
            if (CommonUtil.isEmpty(book.id)) {
                book.id = CommonUtil.createRandomId();
            }
            book = hhyBookRepository.save(book);
            return book;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Path("/{id}")
    @Produces(MediaType.APPLICATION_JSON)
    public HhyBook findOne(@PathParam("id") String id) {
        try {
            HhyBook book = hhyBookRepository.findOne(id);
            if (book == null) {
                JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.NO_CONTENT.getStatusCode());
                return null;
            } else {
                return book;
            }
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<HhyBook> findAll() {
        try {
            List<HhyBook> bookList = hhyBookRepository.findAll();
            return bookList;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAll")
    public Page<HhyBook> findAll(@QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyBook> page = hhyBookRepository.findAll(pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/findAllByTemplateId")
    public Page<HhyBook> findAllByTemplateId(@QueryParam("templateId") String templateId, @QueryParam("pageNumber") int pageNumber, @QueryParam("pageSize") int pageSize) {
        try {
            PageRequest pageRequest = new PageRequest(pageNumber, pageSize);
            Page<HhyBook> page = hhyBookRepository.findAll(pageRequest);
            return page;
        } catch (Exception e) {
            log.error("", e);
            JaxrsUtil.setHttpServletResponseStatusCode(httpServletResponse, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
            return null;
        }
    }
}
